#!/bin/bash  
[ -f $HOME/myconfig ] && . $HOME/myconfig
declare -A usage


_info () {
    echo -e "\e[32m$@\e[39m"
}
_warn () {
    echo -e "\e[93m$@\e[39m"
}
_err () {
    echo -e "\e[31m$@\e[39m"
}


APP_NAME=${1:-$(basename $PWD)}
APP_NAME=fogpodid
#script begin

#description: 
usage[i]="1db init ${APP_NAME} 参数1表示使用pg或者mysql，不传使用H2"
usage[s]="启动用应用 start ${APP_NAME}"
usage[fs]="qianforce-recreate ${APP_NAME}"
usage[ss]="stop-remove-start ${APP_NAME}"
usage[st]="stop ${APP_NAME}"
usage[rm]="remove ${APP_NAME}"
usage[r]="restart ${APP_NAME}"
usage[l]="logs ${APP_NAME}"
usage[lf]="logs following ${APP_NAME}"


ps(){
  docker-compose ps
}



prepareEnv(){
   sed -e "s/#build_time/$(date '+%Y-%m-%d %H:%M:%S')/" env.temp > env
   python -Bc "import ifuncs as i;import sys;i.replace(sys.argv[1])" ${1:-""}
}
i(){
   #sed  -e "s/#gl_host/g.cc/" -e "s/#gl_http_port/$gl_http_port/" -e "s/#gl_https_port/$gl_https_port/" -e "s/#gl_host/g.cc/" -e "s/#gl_host/g.cc/" -e "s/#gl_host/g.cc/"  docker-compose.temp.yml > docker-compose.yml

   2>&1 mkdir -p {mysql,redis}/{data,conf,logs,extensions} >/dev/null
   2>&1 mkdir -p app/{data,conf,logs,extensions,keys} >/dev/null



}
p(){
  docker-compose stop "$@"
}
p1(){
  docker-compose stop $(basename $PWD)
  docker-compose ps
}
n(){
  cd ../..
}
dist(){
    local proj=$(basename $PWD)
    rm2 -Rf dist
     [ ! -d dist ] && mkdir dist
    mkdir dist/$proj/jenkins/data/updates
    #rsync -av sonarqube/extensions dist/$proj/sonarqube
    rsync -av --exclude-from=./exclude_files . dist/$proj
    rsync -av backup/*  dist/$proj
}
r(){
    docker-compose restart
}
db(){
    (cd ../maxkey-web-manage;gradle jibDockerBuild)
    (cd ../maxkey-web-maxkey;gradle jibDockerBuild)
}
dev(){
    rm -Rf ./mysql/data/* ./mysql/init/init_lock ./mysql/conf/master 
    docker-compose up --force-recreate
}
ps(){
   docker-compose ps
}
s(){
   cd $(basename $PWD)
   docker-compose up -d
   docker-compose logs
}
fs(){
    cd $(basename $PWD)
    docker-compose up -d --force-recreate
    docker-compose logs
}
rm2(){
   cd $(basename $PWD)
    docker-compose stop
    docker-compose rm
}
ss(){
    cd $(basename $PWD)
    docker-compose stop
    docker-compose rm
    s
}
df(){
    /bin/rm -Rf dist
    dist
    cd dist/$(basename $PWD)
    ftp

}
ftp(){
  local dir=${1:-$PWD}
  local dirName=$(basename $dir)

  echo 1|xargs -t ncftpput -U 0 -R -v -u $ML_FTP_USER  -P 33256 -p "${ML_FTP_PWD}" ectest.miaozhen.com $dirName $dir/*


  #(cd $dir;ls .|xargs -t -P4 -I {} curl  -u "${ML_FTP_USER}:${ML_FTP_PWD}"  -T {}  "ftp://ectest.miaozhen.com:33256/${dirName}/" -P 33256 --ftp-create-dirs -v)
}
usage(){
    cat - <<EOF
#初始化配置 /var/jenkins_home/updates/default.json
cd /var/jenkins_home/updates
# 替换谷歌地址
sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
# 修改镜像地址
sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json
# 管理插件->高级
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
# 重启
#离线插件安装下载
http://updates.jenkins-ci.org/download/plugins/
EOF

}
si(){
    #自启动运行
    local name=$(basename $PWD)
    sudo cp systemd.service /etc/systemd/system/$name.service
    systemctl disable $name
    sudo systemctl daemon-reload
    systemctl enable $name
}
lf(){
      docker-compose logs -f "$@"
}
l(){
     docker-compose logs "$@"
}
pa(){
    docker-compose stop
}


func=$1
isHelp=$2
MY_DIR=$HOME/my
usage["_r"]="show in finder"
usage["_c"]="open in vs code"
_r(){
    open $MY_DIR/script
}
_c(){
    local editor="vim"
    [ ! -z "$(which subl)" ] && editor="subl"
    [ ! -z "$(which code)" ] && editor="code"
    $editor $MY_DIR/script
}
    if [ "$func" = "-" -o -z "$func" ];then
echo -e "$description"
echo -e "$(_info usage):\n例如: r - 显示帮助信息\n"
for key in "${!usage[@]}";do
    echo -e "$(_info $key): \n\t${usage[$key]}"
done

else


if [ "$(type -t $func)" = "function" ];then 

    shift 
    if [ "$isHelp" = "-"  ];then 
        #usage="${func}_usage"
        #echo -e "$func usage:\n ${!usage}"
        echo -e "$(_info usage:\\n$func)\n  ${usage[$func]}"
        echo "option help"
    else
       $func "$@"
    fi

fi

fi




